package com.cloudminds.nativeapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.cloudminds.nativeapplication.ble.RcListener;
import com.cloudminds.nativeapplication.ble.custome.CustomVoiceGattProvider;
import com.cloudminds.nativeapplication.jni.NativeProcessJni;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    // Used to load the 'native-lib' library on application startup.
    TextView tvKey;
    TextView tvVoice;

    static {
        System.loadLibrary("native-lib");
    }

    public void test(View view) {
        CustomVoiceGattProvider customVoiceGattProvider = new CustomVoiceGattProvider(getApplicationContext());
        customVoiceGattProvider.setRcListener(new RcListener() {
            @Override
            public void onKeyEvent(final int state) {
                Log.d(TAG, "onKeyEvent state:  " + state);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (state == 1) {
                            tvKey.setText("按下");
                        } else if (state == 0) {
                            tvKey.setText("抬起");
                        }
                    }
                });
            }

            @Override
            public void onBuffer(final byte[] audioData) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvVoice.setText(Arrays.toString(audioData));
                    }
                });
            }
        });
        customVoiceGattProvider.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvKey = findViewById(R.id.tv_key);
        tvVoice = findViewById(R.id.tv_voice);

        byte array[] = {'A', 'B', 'C', 'D', 'E'};
        NativeProcessJni.native_process_init(new NativeProcessJni.NativeCallback() {
            @Override
            public void onBuffer(byte[] data) {
                Log.d(TAG, "onBuffer: " + new String(data));
            }
        });

        NativeProcessJni.native_process_feed(array, array.length);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
